package com.service;

import com.dao.UserInfoMybatisDao;
import com.entity.AlertCode;
import com.entity.Dept;
import com.entity.Userinfo;
import com.util.MySqlsessionFactory;
import com.util.MyUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;


public class UserInfoMybatisServiceImpl
        implements UserInfoMybatisService {
    SqlSession sqlSession = MySqlsessionFactory.getSQLSession();
    UserInfoMybatisDao userInfo = (UserInfoMybatisDao) this.sqlSession.getMapper(UserInfoMybatisDao.class);

    public void sqlSession() {
        this.sqlSession.commit();
        this.sqlSession.close();
    }


    public AlertCode userListPage(Map<String, Object> map) {
        System.out.println("showList中的数据" + map);
        Integer page = Integer.valueOf(Integer.parseInt((new StringBuilder()).append(map.get("page")).append("").toString()));
        Integer limit = Integer.valueOf(Integer.parseInt((new StringBuilder()).append(map.get("limit")).append("").toString()));
        page = Integer.valueOf((page.intValue() - 1) * limit.intValue());
        map.put("page", page);
        map.put("limit", limit);
        System.out.println("pagelimit处理后的数据" + map);
        List<Userinfo> userInfoList = this.userInfo.userListPage(map);
        int count = this.userInfo.userCount(map);
        sqlSession();
        if (userInfoList != null && userInfoList.size() > 0) {
            return new AlertCode(0, "", count, userInfoList);
        }

        return new AlertCode(1, "没有符合条件的数据", count, userInfoList);
    }


    public int userCount(Map<String, Object> map, String name) {
        return 0;
    }


    public AlertCode userAdd(Map<String, Object> map) {
        Object obj = map.get("deptno");
        Long deptid = MyUtil.objToLong(obj);
        Map<String, String> map1 = MyUtil.makePasswordSalt((new StringBuilder()).append(map.get("password")).append("").toString());
        Userinfo userinfo = new Userinfo();
        userinfo.setAvatar("zzz");
        userinfo.setDeptno(deptid.longValue());
        userinfo.setPassword(map1.get("password"));
        userinfo.setSalt(map1.get("salt"));
        userinfo.setUsername((new StringBuilder()).append(map.get("username")).append("").toString());
        Integer addNum = this.userInfo.userAdd(userinfo);
        sqlSession();
        if (addNum.intValue() > 0) {
            return new AlertCode(0, "添加成功");
        }
        return new AlertCode(1, "添加成功");
    }


    public AlertCode userDelAll(String str) {
        Integer delNum = this.userInfo.userDelAll(str);
        sqlSession();
        if (delNum.intValue() > 0) {
            return new AlertCode(0, "删除成功");
        }
        return new AlertCode(1, "删除失败");
    }


    public AlertCode userKeyUpdate(int id) {
        List<Userinfo> userinfoList = this.userInfo.userKeyUpdate(id);
        sqlSession();
        if (userinfoList != null && userinfoList.size() > 0) {
            return new AlertCode(0, "", userinfoList);
        }
        return new AlertCode(1, "没有数据");
    }


    public AlertCode userUpdate(Map map) {
        Object obj = map.get("deptno");
        Object objs = map.get("userid");
        Long userid = MyUtil.objToLong(objs);
        Long deptid = MyUtil.objToLong(obj);
        Userinfo userinfo = new Userinfo();
        userinfo.setUserid(userid.longValue());
        userinfo.setDeptno(deptid.longValue());
        userinfo.setUsername((new StringBuilder()).append(map.get("username")).append("").toString());
        Integer addNum = Integer.valueOf(this.userInfo.userUpdate(userinfo));
        sqlSession();
        if (addNum.intValue() > 0) {
            return new AlertCode(0, "修改成功");
        }
        return new AlertCode(1, "修改成功");
    }


    public AlertCode showOption(Map map) {
        List<Dept> showOption = this.userInfo.showOption(map);
        sqlSession();
        if (showOption != null && showOption.size() > 0) {
            return new AlertCode(0, "", showOption);
        }
        return new AlertCode(0, "", showOption);
    }


    public AlertCode login(Map map) {
        List<Userinfo> userInfoList = this.userInfo.login((new StringBuilder()).append(map.get("username")).append("").toString());
        if (userInfoList != null && userInfoList.size() > 0) {
            Userinfo userinfo = userInfoList.get(0);
            String makePasswordSalt = MyUtil.makePasswordSalt((new StringBuilder()).append(map.get("password")).append("").toString(), userinfo.getSalt());
            MyUtil.makePasswordSalt((new StringBuilder()).append(map.get("password")).append("").toString(), userinfo.getSalt());
            if (userinfo.getPassword().equals(makePasswordSalt)) {
                return new AlertCode(0, "登录成功",userInfoList, userInfoList);
            }
            return new AlertCode(1, "账号或者密码不存在");
        }

        return new AlertCode(2, "账号不存在");
    }


    public AlertCode pass(Map map) {
        Object objs = map.get("userid");
        Long userid = MyUtil.objToLong(objs);
        Map<String, String> map1 = MyUtil.makePasswordSalt((new StringBuilder()).append(map.get("password")).append("").toString());
        Userinfo userinfo = new Userinfo();
        userinfo.setUserid(userid.longValue());
        userinfo.setPassword(map1.get("password"));
        userinfo.setSalt(map1.get("salt"));
        Integer pass = this.userInfo.pass(userinfo);
        sqlSession();
        if (pass.intValue() > 0) {
            return new AlertCode(0, "修改成功");
        }

        return new AlertCode(1, "修改失败");
    }
}
